package milk.ui
{
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	
	
	/**
	 * 轻量级preloader,也做了SystemManager 功能.
	 * 
	 *   不好之处,此preloader 变为应该程序的容器;
	 * @author crl
	 * 
	 */	
	public class Preloader extends MovieClip
	{
		public function Preloader()
		{
			stop();
			addEventListener(Event.ADDED_TO_STAGE,addtostageHandler);
		}
		
		private function addtostageHandler(event:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,addtostageHandler);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		private function onEnterFrame(event:Event):void{
			if(framesLoaded == totalFrames)
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				nextFrame();
				var l:Array=currentLabels[1].name.split("_");
				var classFullName:String=l.join(".");
				var cls:Class=getDefinitionByName(classFullName) as Class;
				
				//直接把应用程序加它preloader 中了;
				this.addChild(new cls);
				
				this.complete();
			}
			else
			{
				this.progress(root.loaderInfo.bytesLoaded,root.loaderInfo.bytesTotal);
			}
		}
		
		/**
		 * 进度条; 
		 * @param loaded
		 * @param total
		 * 
		 */		
		protected function progress(loaded:uint,total:uint):void{
			
			var w:int=stage.stageWidth;
			
			graphics.clear();
			var percent:Number =loaded / total;
			graphics.beginFill(0x990000);
			graphics.drawRect(0,0,w*percent,20);
			graphics.endFill();
		}
		
		protected function complete():void{
			graphics.clear();
		}
		
		public function dispose():void{
			//todo;
		}
		
	}
}

